package com.gitee.wsl.compose.ui.richtext.content.span

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.isUnspecified
import com.gitee.wsl.compose.ui.richtext.content.ParagraphContentDrawer
import com.gitee.wsl.compose.ui.richtext.content.ParagraphLayoutInfo

/**
 * Implementation of [ParagraphContentDrawer] to
 * render the paragraph bullet points.
 *
 * @see ParagraphContentDrawer
 * @see BulletSpan
 */
private class BulletParagraphContentDrawer(
    val color: Color,
    val bulletRadius: Float,
    val strokeWidth: Float = Float.NaN
) : ParagraphContentDrawer {

    override fun onDraw(
        drawScope: DrawScope,
        layoutInfo: ParagraphLayoutInfo
    ) {
        val startBottom = layoutInfo.result.getLineBottom(layoutInfo.startLine)
        val (radius, style) = if (strokeWidth.isNaN()) {
            bulletRadius to Fill
        } else {
            (bulletRadius - strokeWidth / 2f) to Stroke(strokeWidth)
        }

        drawScope.drawCircle(
            color = if (color.isUnspecified) {
                layoutInfo.result.layoutInput.style.color
            } else {
                color
            },
            center = Offset(
                x = layoutInfo.x + layoutInfo.dirSign * bulletRadius,
                y = (layoutInfo.top + startBottom) / 2f
            ),
            radius = radius,
            style = style
        )
    }
}